package labb6;

   import java.io.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.net.*;
import java.util.ArrayList;
   import javax.swing.*;
   import javax.swing.event.*;
   import javax.swing.table.DefaultTableModel;
   import javax.swing.text.html.*;
   import javax.swing.text.*;

public class mainGUI extends JFrame implements ActionListener, HyperlinkListener {
   Bookmark bookmark;
   Container contentPane;
   JPanel main, top, buttonPanel;
   JScrollPane links, browser2;
   Webreader browser;
   JTable table;
   JTextField adressBar;
   String[][] linkArray;
   String[] namn = {"Namn", "Adress"};
   String previousURL, url;
   JButton forward, back, bookmarks;
   linkTable linkTable;
   history his;
   
   
   public mainGUI(){
       contentPane = this.getContentPane();
       contentPane.setLayout(new BorderLayout());
       his = new history();
       linkTable = new linkTable();
       
       browser = new Webreader();
       browser.addHyperlinkListener(this);
       
       table = new JTable(50,2);
       links = new JScrollPane(table);
       browser2 = new JScrollPane(browser);
       adressBar = new JTextField(80);
       adressBar.addActionListener(this);
       bookmark = new Bookmark();
       //bookmark.setVisible(false);
       
       defineBtns();
       drawingGUI();
       
   }
   @Override
   public void actionPerformed(ActionEvent e){
       url = adressBar.getText();

       his.addPast(browser.getURL());
       his.clearFuture();
       go(url);
       
   }
   
   private void drawingGUI(){
       buttonPanel = new JPanel(new FlowLayout(0,1,0));
       buttonPanel.add(back);
       buttonPanel.add(forward);
       
       top = new JPanel(new BorderLayout());
       top.add(buttonPanel, BorderLayout.WEST);
       top.add(adressBar, BorderLayout.CENTER);
       top.add(bookmarks, BorderLayout.EAST);
       
       
       contentPane.add(links, BorderLayout.EAST);
       contentPane.add(browser2, BorderLayout.CENTER);
       contentPane.add(top, BorderLayout.NORTH);
       
       setSize(new Dimension(1200, 600));
       setVisible(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   
   private void defineBtns(){
       forward = new JButton("Forward");
       forward.setEnabled(false);
       forward.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e){
           
                his.addPast(browser.getURL());
                url = his.forward();
                go(url);
           }
       });
       back = new JButton("Back");
       back.setEnabled(false);
       back.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e){
               
               his.addFuture(browser.getURL());
               url = his.back();
               go(url);               
           }
       });
       
       bookmarks = new JButton("Bookmarks");
       bookmarks.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e){
               bookmark.newUrl(browser.getURL());
               bookmark.setVisible(true);
           }
       });

   }
   public static void main(String[] args){
       new mainGUI(); 
   
   }
   
   void go(String goURL){
       browser.goTo(goURL);
       linkArray = linkTable.getLinks(goURL);
       table.setModel(new DefaultTableModel(linkArray, namn));
       adressBar.setText(goURL);
       back.setEnabled(!his.isPastEmpty());
       forward.setEnabled(!his.isFutureEmpty());
   }

    @Override
    public void hyperlinkUpdate(HyperlinkEvent e) {
      if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        his.addPast(browser.getURL());
        go(e.getURL().toExternalForm());
        adressBar.setText(e.getURL().toExternalForm());
      }
    }
}

   
